//
//  CTPresentationController.swift
//  Swift-Tools
//
//  Created by gree on 2020/10/16.
//

import UIKit
import SnapKit

class CTPresentationController: UIPresentationController {

    /// 透明视图
    fileprivate var dimmingView: UIView!
    /// 入场方向
    private var direction: CTPresentationDirection = .bottom()
    
    init(presentedViewController: UIViewController,
         presenting presentingViewController: UIViewController?,
         direction: CTPresentationDirection) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
        setupDimingView()
        presentedViewController.transitioningDelegate = self
        self.direction = direction
    }
    
    func setupDimingView() {
        dimmingView = UIView()
        dimmingView.translatesAutoresizingMaskIntoConstraints = false
        dimmingView.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
        dimmingView.alpha = 0
        
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(dimmingViewHandleTap))
        dimmingView.addGestureRecognizer(recognizer)
    }
    
    @objc func dimmingViewHandleTap() {
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}
extension CTPresentationController : UIViewControllerTransitioningDelegate {
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        return CTSlideInPresentationAnimator(direction: direction, isPresentation: false)
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CTSlideInPresentationAnimator(direction: direction, isPresentation: true)
    }
}

extension CTPresentationController {
    
    override func presentationTransitionWillBegin() {
        
        containerView?.insertSubview(dimmingView, at: 0)
        
        dimmingView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalTo(0)
        }
        
        guard let coordinator = presentedViewController.transitionCoordinator else {
            dimmingView.alpha = 1.0
            return
        }
        
        coordinator.animate { (_) in
            self.dimmingView.alpha = 1.0
        }
    }
    
    override func dismissalTransitionWillBegin() {
        guard let coordinator = presentedViewController.transitionCoordinator else {
            dimmingView.alpha = 0.0
            return
        }
        
        coordinator.animate { (_) in
            self.dimmingView.alpha = 0.0
        }
    }
    
    override func dismissalTransitionDidEnd(_ completed: Bool) {
        
    }
    
    override func containerViewWillLayoutSubviews() {
        presentedView?.frame = frameOfPresentedViewInContainerView
    }
    
    override func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize {
        return parentSize
    }
    
    override var frameOfPresentedViewInContainerView: CGRect {
        var frame: CGRect = .zero
        if let s = containerView?.bounds.size {
            frame.size = size(forChildContentContainer: presentedViewController, withParentContainerSize: s)
            
            switch direction {
                case .top(_, let height):
                    frame.origin.y = 0
                    frame.size.height = height
                case .bottom(_, let height):
                    frame.origin.y = s.height - height
                    frame.size.height = height
                case .left(let width , _):
                    frame.origin.x = 0
                    frame.size.width = width
                case .right(let width, _):
                    frame.origin.x = s.width - width
                    frame.size.width = width
            }
        }
        print(frame)
        return frame
    }
}

